package com.example.main;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;

import java.nio.charset.StandardCharsets;

public class Main03_Common_Digest {

    public static void main(String[] args) {
//        System.out.println(encryptToBase64("123456"));
        System.out.println(decryptBase64("MTIzNDU2"));
    }

    public static String encryptToMD5(String str) {
        return DigestUtils.md5Hex(str);
    }

    public static String encryptToHmacSha256(String str) {
        return new HmacUtils(HmacAlgorithms.HMAC_SHA_256,"key").hmacHex(str);
    }

    public static String encryptToBase64(String str) {
        return Base64.encodeBase64String(str.getBytes(StandardCharsets.UTF_8));
    }

    public static String decryptBase64(String str) {
        if (Base64.isBase64(str)){
            byte[] bytes = Base64.decodeBase64(str);
            return new String(bytes,StandardCharsets.UTF_8);
        }
        return null;
    }
}
